<?php
/*-----------------------------------------------------
    NewsLister admin.php
	
    @package NewsLister
	@since 1.0
	
	@version    1.0.0
	@copyright Tenderfeel
	@author Tenderfeel(tenderfeel@gmail.com)
	@link http://tenderfeel.xsrv.jp/ 
------------------------------------------------------*/

require_once("include/config.php");


if(isset($_POST['action'])&&$_POST['action']=="logout"){
	//セッションクッキー削除
	if (isset($_COOKIE[COOKIENAME])) {
		setcookie(COOKIENAME, '', time()-42000);
	}
	header("Location:".BACKURL);
}

if( isset($_COOKIE[COOKIENAME])&&$_COOKIE[COOKIENAME]== substr(md5(PASSWORD.SEACRETKEY),4,11)){
	
	admin_panel();
	
}else{
	html_header("Login","Login");
	if(empty($_GET["mode"])&&isset($_POST['action'])&&$_POST["action"]=="login"&& isset($_POST['loginpass'])){
		if(strip_tags($_POST['loginpass']) === PASSWORD) {
			setcookie(COOKIENAME,substr(md5(PASSWORD.SEACRETKEY),4,11),time()+(3600*24*14));
			header('Location:'.$PHP_SELF."?mode=post");
		}else{
			print "<p class=\"error\">パスワードが違います</p>";
		}
	}
	html_login();
	html_footer();
}

	
/**
 *　管理画面
 */
function admin_panel(){
	
	html_header("管理ページ");
	
	$today = getdate();
	$time = time();
	$m = 1;
	print "<div id=\"main\">";
	print "<div id=\"message\"></div>";
	print "<div id=\"postarea\">";
	print "<h2>NewPost</h2>\n";
	print "<fieldset id=\"postform\">\n";
	print "<legend>Post Form</legend>\n";
	print "<p class=\"dateline\">\n";
	print '<label><input id="year" type="text" size="4" maxlength="4" value="'.$today["year"].'" />年</label>'."\n";
	
	print '<select id="month" name="month">';
			for ($i=0; $i<12; $i++) {
				$md = sprintf("%02d",$m);
				if($m==$today["mon"]){
					echo("<option value=\"$md\" selected=\"selected\">".$m."月</option>\n");
				}else{
					echo("<option value=\"$md\">".$m."月</option>\n");
				}
				$m++;
			}
	print '</select>';
	
	print '<label><input id="day" type="text" size="2" maxlength="2" value="'.$today["mday"].'" />日</label>'."\n";
	print " @ ";
	print '<input id="hour" type="text" size="2" maxlength="2" value="'.$today["hours"].'" /> : '."\n";
	print '<input id="min" type="text" size="2" maxlength="2" value="'.$today["minutes"].'" />'."\n";
	print "</p>\n";
	print '<p class="textarea"><textarea id="comment" cols="40" rows="3"></textarea></p>'."\n";
	print '<p class="button"><button type="button" id="save">Save</button></p>';
	print "</fieldset>\n";
	print "</div>\n";
	print "<span id=\"reload\">Reload</span>\n";
	print "<div id=\"logarea\">\n";
	print "</div>\n";
	print "</div>\n";
	
	html_footer();
}

/**
 * PHP環境設定
 */
function init(){
	ini_set("output_buffering", "On");
	ini_set("output_handler", "mb_output_handler");
	ini_set("default_charset", "UTF-8");
	mb_language("Japanese");
	mb_internal_encoding ("UTF-8");
	mb_http_input("auto");
	mb_http_output("UTF-8");
	mb_detect_order("auto");
	mb_substitute_character("none");
	ob_start("mb_output_handler");
	
	header("Cache-Control: no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache"); 

}

/**
 * HTMLヘッダー
 */
function html_header($title=null,$sub=null) {
init();
print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
print "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
print "<head>\n";
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
print "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n";
if(isset($_COOKIE[COOKIENAME])){
print '<script type="text/javascript" src="include/mootools-1.2.4.js"></script>'."\n";
print '<script type="text/javascript" src="include/admin.js"></script>'."\n";
}
echo '<link rel="stylesheet" type="text/css" href="include/style.css" />'."\n";
print '<title>'.$title ." &raquo; ".SCRIPT.'</title>'."\n";
print '</head>'."\n";
print '<body>'."\n";
print "<div id=\"container\">\n";
print "<div id=\"header\">\n";
print '<h1 class="banner"><a href="index.html" class="siteName" title="HOME">'.SCRIPT.'</a></h1>'."\n";
if(isset($_COOKIE[COOKIENAME])){
	print "<form method=\"post\" action=\"\">\n";
	print "<input type=\"hidden\" name=\"action\" value=\"logout\" />\n";
	print "<button type=\"submit\" id=\"logout\">Logout</button>\n";
	print "</form>\n";
}
print "</div>\n";

}
/**
 * HTMLフッター
 */
function html_footer(){
	print "<div id=\"footer\"><address>".SCRIPT."&nbsp;".VERSION." &copy; Copyright 2009 <a href=\"http://tenderfeel.xsrv.jp/\">Tenderfeel</a> All Rights Reserved.</address>\n";
	print "</div>\n</body>\n";
	print "</div>\n";
	print "</html>\n";
}
/**
 * ログインフォーム
 */
function html_login() {
echo <<< END_OF_HTML
	<fieldset id="login">
	<legend><img src="include/images/hn_login.png" width="60" height="25" alt="login" /></legend>
	<form method="post" action="">
	<p><input type="hidden" name="action" value="login" />
	<label for="loginpass">Password:</label><input type="password" id="loginpass" name="loginpass" />
	<button type="submit" id="login">Login</button></p>
	</form>
	</fieldset>
END_OF_HTML;
}
?>